<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>装修百科管理</el-breadcrumb-item>
      <el-breadcrumb-item>装修百科详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <template>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="百科信息" name="pedia_info">
            <template>
              <!-- :rules="rules" -->
              <el-form :model="adornPediaForm" ref="adornPediaForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="百科名称" required style="width: 600px;" prop="name">
                  <el-input v-model="adornPediaForm.name"></el-input>
                </el-form-item>
                <el-form-item label="百科类型" required prop="category">
                  <el-select v-model="adornPediaForm.category" clearable placeholder="请选择分类">
                    <el-option v-for="item in adornPediaDictData.category" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="百科图标" required style="width: 600px;" prop="name">
                  <el-input v-model="adornPediaForm.icon"></el-input>
                </el-form-item>
                <el-form-item label="百科颜色" required style="width: 600px;" prop="name">
                  <el-input v-model="adornPediaForm.color"></el-input>
                </el-form-item>
                <el-form-item label="百科摘要" required style="width: 800px;" prop="name">
                  <el-input type="textarea" style="height: 93px;" v-model="adornPediaForm.abstract" :autosize="{ minRows: 4, maxRows: 4}"></el-input>
                </el-form-item>
                <el-form-item label="百科内容" required style="width: 1200px;" prop="name">
                  <el-input type="textarea" style="height: 500px;" v-model="adornPediaForm.content" :autosize="{ minRows: 22, maxRows: 22}"></el-input>
                </el-form-item>
                <el-form-item label="图片" required prop="thumbnail">
                  <el-upload class="upload-demo" accept="image/*" :headers="uploadParams.uploadHeaders" :data="uploadParams.uploadData"
                    :action="uploadParams.uploadFileUrl" :on-success="uploadSuccess">
                    <el-button size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
                <div v-if="adornPediaForm.thumbnail_path != ''" style="height: 150px;width: 300px;margin-left: 100px;margin-bottom: 20px;">
                  <el-image style="height: 100%;" :src="adornPediaForm.thumbnail_path" fit="fill"></el-image>
                </div>
                <el-form-item>
                  <el-button type="primary" @click="updateOrCreateAdornPediaForm">保存</el-button>
                  <el-button v-if="operate == 'add'" @click="resetForm">重置</el-button>
                </el-form-item>
              </el-form>
            </template>
          </el-tab-pane>
          <el-tab-pane label="百科内容图片" name="pedia_content">
            <el-upload class="upload-demo" :action="uploadParams.uploadFileUrl" :data="uploadParams.uploadData"
              :headers="uploadParams.uploadHeaders" :before-upload="uploadCheck" :on-success="pediaContentaddImgUploadSuccess">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <div style="margin-top: 10px;margin-bottom: 100px;">
              <div style="display: flex;flex-wrap:wrap ;">
                <div v-for="image in adornPediaForm.content_images" :key="image.id" style="width: 300px;height: 150px;border: 1px solid #aaaa7f;margin: 10px;">
                  <el-image style="height: 100%;" :src="image.file_path" fit="fill"></el-image>
                  <div style="word-break:break-all">{{image.file_path}}</div>
                </div>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="百科图片" name="pedia_images">
            <el-upload class="upload-demo" :action="uploadParams.uploadFileUrl" :data="uploadParams.uploadData"
              :headers="uploadParams.uploadHeaders" :before-upload="uploadCheck" :on-success="pediaAddImgUploadSuccess">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <div style="margin-top: 10px;">
              <div style="display: flex;flex-wrap:wrap ;">
                <div v-for="image in adornPediaForm.images" :key="image.id" style="width: 300px;height: 150px;border: 1px solid #aaaa7f;margin: 10px;">
                  <el-image style="height: 100%;" :src="image.file_path" fit="fill"></el-image>
                </div>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="关联产品" name="relact_product">
            <template>
              <el-transfer style="text-align: left; display: inline-block" v-model="adornPediaForm.relate_products"
                filterable :left-default-checked="[2, 3]" :right-default-checked="[1]" :titles="['Source', 'Target']"
                :format="{
                      noChecked: '${total}',
                      hasChecked: '${checked}/${total}'
                    }"
                :data="relateProductData">
                <span slot-scope="{ option }">{{ option.label }}</span>
              </el-transfer>
            </template>
            <el-button type="primary" @click="addRelateProduct">保存</el-button>
          </el-tab-pane>
        </el-tabs>
      </template>
    </el-card>
  </div>
</template>

<script>
  import {
    getUploadParamsApi,
    _getAdornPediaDetailApi,
    getAdornPediaDictApi,
    updateOrCreateAdornPediaApi,
    _addPediaImageApi,
    _addPediaContentImageApi,
    _getrelateProductListApi,
    _adornPediaRelectProductApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        operate: 'add',
        adornPediaDictData: {}, // 产品分类
        relateProductData: [],
        adornPediaForm: {
          id: -1,
          name: '',
          icon: '',
          color: '',
          category: null,
          abstract: '',
          content: '',
          thumbnail: null
        },
        // 文件数据
        uploadParams: {},
        // 页面样式
        activeName: 'relact_product'
      }
    },
    created() {
      this.getAdornPediaDict()
      this.getrelateProductListApi()
      // 获取文件上传的参数配置
      this.uploadParams = getUploadParamsApi('adorn_pedia')
      // 判断是create or update
      const pediaId = this.$route.query.pediaId
      if (pediaId !== 'add') {
        this.operate = 'edit'
        this.getAdornPedia(pediaId)
      }
    },
    methods: {
      // 获取分类数据
      async getAdornPediaDict() {
        const {
          data: res
        } = await getAdornPediaDictApi()
        console.log(res)
        this.adornPediaDictData = res.data
      },
      async getrelateProductListApi() {
        const {
          data: res
        } = await _getrelateProductListApi()
        this.relateProductData = res.data.dataList
      },
      async getAdornPedia(pediaId) {
        const {
          data: res
        } = await _getAdornPediaDetailApi(pediaId)
        this.adornPediaForm = res.data
        // this.relate_products = res.data.relate_products
      },
      async addRelateProduct() {
        const {
          data: res
        } = await _adornPediaRelectProductApi(this.adornPediaForm.id, this.adornPediaForm.relate_products)
        if (res.status) {
          this.$message.success('关联成功！')
        }
      },
      // 创建更新
      updateOrCreateAdornPediaForm() {
        this.$refs.adornPediaForm.validate(async valid => {
          if (valid) {
            const {
              data: res
            } = await updateOrCreateAdornPediaApi(this.adornPediaForm, this.operate)
            console.log(res)
            if (res.status) {
              var message = this.operate === 'add' ? '添加成功！' : '编辑成功！'
              this.$message.success(message)
              if (this.operate === 'add') {
                this.operate = 'edit'
                this.adornPediaForm.id = res.data
              }
            } else {
              this.$message.error('操作失败！')
            }
          }
        })
      },
      // 重置表单
      resetForm() {
        this.$refs.adornPediaForm.resetFields()
      },
      // 文件上传
      uploadSuccess(response) {
        if (response.status) {
          // 保存id 和显示的图片地址
          this.adornPediaForm.thumbnail = response.data.id
          this.adornPediaForm.thumbnail_path = response.data.file_path
        }
      },
      // 图片s
      uploadCheck() {
        if (this.adornPediaForm.id < 0) {
          this.$message.error('请先保存产品信息,在上传图片')
          return false
        }
        return true
      },
      async pediaContentaddImgUploadSuccess(response) {
        const {
          data: res
        } = await _addPediaContentImageApi(this.adornPediaForm.id, response.data.id)
        if (res.status) {
          this.getAdornPedia(this.adornPediaForm.id)
          this.$message.success('添加成功')
        }
      },
      async pediaAddImgUploadSuccess(response) {
        console.log(response)
        const {
          data: res
        } = await _addPediaImageApi(this.adornPediaForm.id, response.data.id)
        if (res.status) {
          this.getAdornPedia(this.adornPediaForm.id)
          this.$message.success('添加成功')
        }
      },
      // 标签页事件
      handleClick(tab, event) {
        this.activeName = tab.name
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
